home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / CGIshell 1.3.2 / Pocket 6.5 / Extensions / DataFiles.pf next >
Text File  |  1995-11-11  |  5KB  |  135 lines

  1. ( dataFiles 16 November 1995 - a possible scheme for using data files )
  2. 
  3. \ ======================= DataFiles =============================
  4.  
  5. \ Because they are available on all Macs and somewhat easier to implement,
  6. \ File Control Blocks are used instead of the more modern fss records.
  7.  
  8. \    An FCB has the following format:
  9. \       offset  name          size   description
  10. \         12    ioCompletion   4     routine address
  11. \         16    ioResults      2     result (error) code
  12. \         18    ioNamePtr      4     pathname address - STRING[255]
  13. \         22    ioVRefNum      2     volume ref. number
  14. \         24    ioRefNum       2     file ref. num
  15. \         26    ioVersNum      1     ?
  16. \         27    ioPermssn      1     write permission - set for read only
  17. \         28    ioMisc         4     end of file & ?
  18. \         32    ioBuffer       4     address of read/write buffer
  19. \         36    ioReqCount     4     bytes to read/write
  20. \         40    ioActCount     4     actual bytes read/written
  21. \         44    ioPosMode      2     read/write positionning mode
  22. \         46    ioPosOffset    4     offset into the file
  23. \       plus more stuff that isn't shown here. See Inside Mac
  24.  
  25. \ File errors:
  26. \         -35  no such volume
  27. \         -36  i/o error
  28. \         -37  bad filename
  29. \         -38  file not open
  30. \         -39  logical eof reached
  31. \         -40  mark position error
  32. \         -42  too many files open
  33. \         -43  file not found
  34. \         -44  hardware volume lock
  35. \         -45  file locked
  36. \         -46  volume locked check box checked
  37. \         -48  duplicate filename
  38. \         -49  file already open
  39. \         -50  negative count requested
  40. \         -51  bad file ref. no.
  41. \         -61  no permission
  42.  
  43. \ create space for the fcb and a word to access it
  44. variable FCB 78 allot  ( our File's Control Block )
  45. : +FCB ( offset -- addr ) fcb + ;  ( offset into fcb )
  46. : FTRAP ( -- ) fcb >abs  ,$ 205E ;  ( movea.l [ps]+,a0 )
  47.  
  48. : CLOSE ( -- ) ftrap ,$ A001  ftrap ,$ A013 ;  ( _Close & _FlushBuffer )
  49.  
  50. \ error handling
  51. : .ERROR ( error# -- ) ." Disk Error" .  close  abort ;
  52. variable ^ERROR  ' .error ^error !
  53. : DERROR ( -- error# ) 16 +fcb @ ;
  54. : ?DERROR ( -- ) derror ?dup IF  ^error @ execute THEN ;
  55.  
  56. \ set file descriptors
  57. : !FILE ( string[255] wd# - )
  58.     fcb 80 0 fill      \    clear the fc buffer
  59.     22 +fcb  !          \   set vRefnum to n (zero means same folder)
  60.     >abs  18 +fcb  2! ;  \  set name to string
  61.  
  62. \ open a file
  63. : OPENFILE ( string[255] wd# - ) !file ftrap ,$ A000 ?derror ;  \ _HOpen
  64. : OPEN ( string[255] -- ) 0 openfile ; \ for the lazy: file in same folder
  65.  
  66. \ check a file
  67. : ?FILEOK ( string[255] wd# -- flag ) \ true if exists, has permission, etc.
  68.     !file ftrap ,$ A000  derror 0= close ;
  69.  
  70. \ create a file
  71. : NEWFILE ( string[255] wd# -- )
  72.     !file ftrap ,$ A008  ?derror  ( _Create )
  73.     ,s TEXT 32 +fcb 2!             \ TEXT type
  74.     ftrap ,$ A00D ?derror ;       ( _SetFileInfo )
  75.  
  76. \ delete a file
  77. : DELFILE ( string[255] wd# -- )
  78.     !file ftrap ,$ A009 ?derror ;  ( _Delete )
  79.  
  80. \ return the filesize !!! MUST BE <32K !!!
  81. : @SIZE ( -- bytes ) ftrap ,$ A011  30 +fcb @ ;  ( _GetEOF )
  82.  
  83. \ set some fcb parameters
  84. : !SIZE ( bytes -- ) 38 +fcb ! ;      \ set bytes-to-read/write
  85. : !BUFF ( addr -- ) >abs 32 +fcb 2! ;  \ set read/write buffer pointer
  86.  
  87. \ read/write with buffer addr and bytes to read/write on the stack
  88. : READ ( addr count -- )  !size !buff ftrap ,$ A002  ?derror ;  ( _Read )
  89. : WRITE ( addr count -- ) !size !buff ftrap ,$ A003  ?derror ;  ( _Write )
  90.  
  91. \ read until character, c, is encountered
  92. : CREAD ( addr c -- bytes_read ) \ See Inside Mac for how this works
  93.     44 +fcb c!  128 45 +fcb c!  \ setup ioPosMode
  94.     @size read  42 +fcb @ ;     \ put lowbyte of ioActCount on stack
  95.  
  96. \ read/write file a byte at a time to/from the stack
  97. : GETCHR ( -- c ) here 1 read  here c@ ;
  98. : PUTCHR ( c -- ) here c!  here 1 write ;
  99.  
  100. \ -------------------Standard Reply Dialogs-------------------
  101.  
  102. variable SFREPLY 72 allot
  103. : SFDATA ( -- filename wd# true  OR  false )
  104.     sfreply @ IF  sfreply 10 +  sfreply 6 + @  -1 ELSE  0  THEN ;
  105.  
  106. : SFPUTFILE ( defaultname prompt -- filename wd# -1 OR zero )
  107.     sfreply 74 0 fill
  108.     55 75 2>r  \     top left corner
  109.     a>r  a>r    \    prompt.string  default.filename.string
  110.     0 0 2>r      \   dialog hook pointer
  111.     sfreply a>r   \  reply record address
  112.     1 >r ,$ a9ea   \ _SFPutFile
  113.     sfdata ;
  114.  
  115. : SFGETFILE ( -- filename.string[255] wd# -1 OR zero )
  116.     sfreply 74 0 fill
  117.     55 75 2>r    \     top left corner
  118.     0 0 2>r       \    unused
  119.     0 0 2>r        \   filter procPtr
  120.     1 >r            \  one type in the type list
  121.     ,s TEXT SP@ 2>r  \ keep the type list on the stack
  122.     0 0 2>r           \   dialog hook pointer
  123.     sfreply a>r        \  reply record
  124.     2 >r ,$ A9EA        \ _SFGetFile
  125.     2drop           \  get rid of the stacked type list
  126.     sfdata ;
  127.  
  128.  
  129. \ --------------------LIST - a quick demo --------------------
  130.  
  131. : LIST ( -- )
  132.     sfgetfile IF  openfile 
  133.       @size 0 DO  getchr emit  LOOP
  134.     close THEN ;
  135.